package com.education.webservlet;

import com.education.db.HibernateSessionFactory;
import com.education.db.dao.InfoCatagoryDAO;
import com.education.db.dao.InfoCourseDAO;
import com.education.db.dao.InfoMajorDAO;
import com.education.db.dao.InfoSchoolDAO;
import com.education.db.domain.InfoCatagory;
import com.education.db.domain.InfoCourse;
import com.education.db.domain.InfoMajor;
import com.education.db.domain.InfoSchool;
import com.education.service.bean.ActivityBean;
import org.hibernate.Transaction;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fan307 on 2016/11/24.
 */
@WebServlet(name = "VerifyFilesServlet")
public class VerifyFilesServlet extends HttpServlet {
    private Transaction tran;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        tran = HibernateSessionFactory.getSession().getTransaction();
        try{
            tran.begin();
            InfoCatagoryDAO infoCatagoryDAO = new InfoCatagoryDAO();
            InfoSchoolDAO infoSchoolDAO = new InfoSchoolDAO();
            InfoMajorDAO infoMajorDAO = new InfoMajorDAO();
            InfoCourseDAO infoCourseDAO = new InfoCourseDAO();
            List<InfoCatagory> infoCatagories = infoCatagoryDAO.findAll();
            List<ActivityBean> infoCatagoryBean = new ArrayList<>();
            List<InfoSchool> infoSchools = infoSchoolDAO.findAll();
            List<ActivityBean> infoSchoolBean = new ArrayList<>();
            List<InfoMajor> infoMajors = infoMajorDAO.findAll();
            List<ActivityBean> infoMajorBean = new ArrayList<>();
            List<InfoCourse> infoCourses = infoCourseDAO.findAll();
            List<ActivityBean> infoCourseBean =new ArrayList<>();
            for(InfoCatagory infoCatagory:infoCatagories)
            {
                ActivityBean infocatagorybean = new ActivityBean();
                infocatagorybean.setId(infoCatagory.getCaId());
                infocatagorybean.setValue(infoCatagory.getCaValue());
                infocatagorybean.setName(infoCatagory.getCaName());
                infoCatagoryBean.add(infocatagorybean);
            }
            for(InfoSchool infoSchool:infoSchools)
            {
                ActivityBean infoschoolbean = new ActivityBean();
                infoschoolbean.setId(infoSchool.getScId());
                infoschoolbean.setValue(infoSchool.getScValue());
                infoschoolbean.setName(infoSchool.getScName());
                infoSchoolBean.add(infoschoolbean);
            }
            for(InfoMajor infoMajor:infoMajors)
            {
                ActivityBean infomajorbean = new ActivityBean();
                infomajorbean.setId(infoMajor.getMaId());
                infomajorbean.setValue(infoMajor.getMaValue());
                infomajorbean.setName(infoMajor.getMaName());
                infoMajorBean.add(infomajorbean);
            }
            for(InfoCourse infoCourse:infoCourses)
            {
                ActivityBean infomajorbean = new ActivityBean();
                infomajorbean.setId(infoCourse.getCoId());
                infomajorbean.setValue(infoCourse.getCoValue());
                infomajorbean.setName(infoCourse.getCoName());
                infoCourseBean.add(infomajorbean);
            }
            request.setAttribute("infocatagorybean",infoCatagoryBean);
            request.setAttribute("infoschoolbean",infoSchoolBean);
            request.setAttribute("infomajorbean",infoMajorBean);
            request.setAttribute("infocoursebean",infoCourseBean);
            request.getRequestDispatcher("/admin/verifyFiles.jsp").forward(request,response);
            tran.commit();
        }catch(Exception e)
        {
            e.printStackTrace();
            System.out.println(e.toString());
            tran.rollback();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}